package at.fhj.itm;

/**
 * Queue class to represent a queue with a count of elements.
 * Its possible to insert or remove elements.
 * If the Queue is full,
 * 
 * 
 * @author hannesfeichtl
 *
 */

public class Queue {
	
  private int maxSize;

  private long[] queArray;

  private int front;

  private int rear;

  private int nItems;

  
  /**
   * Creates new Queue with size s and inits values.
   * 
   * @param s Integer to set size of Queue
   */
  public Queue(int s) {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
  }

  /**
   * Inserts element j into the queue, if the queue is full the first item is replaced by the new item.
   * 
   * @param j long element to insert into the queue
   */
  //   put item at end of a queue
  public void insert(long j) {
    if (rear == maxSize - 1) // deal with wraparound
      rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; 
  }

  /**
   * Removes and returns front element of the queue
   * 
   * @return long current front element
   */
  //   take item from front of queue
  public long remove() {
    long temp = queArray[front++]; // get value and incr front
    if (front == maxSize) // deal with wraparound
      front = 0;
    nItems--; // one less item
    return temp;
  }

  /**
   * Returns front element of the queue
   * 
   * @return long current front element
   */
  public long peekFront() {
    return queArray[front];
  }

  /**
   * Checks if the Queue is empty. Returns true if its empty, false if its not empty.
   * 
   * @return boolean if the Queue is empty.
   */
  public boolean isEmpty() {
    return (nItems == 0);
  }

  /**
   * Checks if the Queue is full. Returns true if its full, false if its not full.
   * 
   * @return boolean if the Queue is full.
   */
  public boolean isFull() {
    return (nItems >= maxSize);
  }

  /**
   * Returns count of Items in the Queue.
   * 
   * @return integer how many Items are in the Queue
   */
  public int size() {
    return nItems;
  }

  /**
   * Demo main method to test Queue class.
   * Creates a Queue class with 5 elements.
   * Inserts 4 elements, then deletes 3 and inserts another 4.
   * Removes and prints all elements as long as Queue is not empty.
   * 
   * 
   * @param args
   */
  // TODO Refactor / Delete Main Method and create JUnit test code
  public static void main(String[] args) {
    Queue theQueue = new Queue(5); // queue holds 5 items

    theQueue.insert(10);
    theQueue.insert(20);
    theQueue.insert(30);
    theQueue.insert(40);

    theQueue.remove(); 
    theQueue.remove(); 
    theQueue.remove();

    theQueue.insert(50); 
    theQueue.insert(60); //    (wraps around)
    theQueue.insert(70);
    theQueue.insert(80);

    while (!theQueue.isEmpty()) {
      long n = theQueue.remove(); // (40, 50, 60, 70, 80)
      System.out.print(n);
      System.out.print(" ");
    }
    System.out.println("");
  }
}
           
         